package util.view.player
{
    import ca.turbulent.media.Pyro;
    
    import core.manager.*;
    
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;
    
    import memory.*;
    
    import mx.core.*;
    
    import util.view.player.event.*;
    
    import view.fl2mx.Fl2Mx;

    public class FLVPlayer extends UIComponent
    {
        private var videoHeight:int;
        private var autoPlay:Boolean;
        private var videoWidth:int;
        private var hasCloseButton:Boolean;
        private const maxTries:int = 3;
        private var netConnection:NetConnection;
        private var path:String;
        private var canSkip:int;
        private var numTries:int = 0;
        private var close:BTNClose;
        private var video:Video;
        private var netStream:NetStream;

        public function FLVPlayer(path:String, videoWidth:int,videoHeight :int,hasCloseButton :Boolean = true,autoPlay :Boolean = false,canSkip :int = 0)
        {
            this.path = path;
            this.videoWidth =videoWidth;
            this.videoHeight =videoHeight;
            this.hasCloseButton =hasCloseButton;
            this.autoPlay =autoPlay;
            this.canSkip =canSkip;
            initNetConnection();
            return;
        }// end function

        private function netConnection_NetStatus_handler(type:NetStatusEvent) : void
        {
            if (type.info.code == "NetConnection.Connect.Success")
            {
                netStream = new NetStream(netConnection);
                netStream.client = new Object();
                netStream.checkPolicyFile = true;
                video = new Video(this.videoWidth, this.videoHeight);
                video.attachNetStream(netStream);
                this.addChild(video);
                netStream.play(this.path);
                if (!this.autoPlay)
                {
                    netStream.pause();
                }
                else if (canSkip == 1)
                {
                    this.initBTNClose();
                }
                netStream.addEventListener(NetStatusEvent.NET_STATUS, netStream_NetStatusHandler);
                this.addEventListener(Event.ENTER_FRAME, on_ENTER_FRAME);
                this.addEventListener(Event.ENTER_FRAME, checkIsReady);
            }
            return;
        }// end function

        public function getNetStream() : NetStream
        {
            return netStream;
        }// end function

        public function getVideo() : Video
        {
            return video;
        }// end function

        public function restart() : void
        {
            if (netStream != null)
            {
                netStream.seek(0);
                resume();
            }
            return;
        }// end function

        private function onEXIT_FRAME(type:Event) : void
        {
            if (netStream != null)
            {
                netStream.resume();
            }
            return;
        }// end function

        public function pause() : void
        {
            if (netStream != null)
            {
                netStream.pause();
            }
            return;
        }// end function

        private function netStream_NetStatusHandler(type:NetStatusEvent) : void
        {
            var e:* = type;
            if (e.info.code == "NetStream.Play.Stop")
            {
                this.dispatchEvent(new NetStatusEvent(NetStatusEvent.NET_STATUS));
            }
            if (e.info.code == "NetStream.Play.StreamNotFound")
            {
                if (this.numTries < this.maxTries)
                {
                    this.numTries = this.numTries++;
                    try
                    {
                        netStream.play(this.path);
                        netStream.pause();
                    }
                    catch (e:Error)
                    {
                    }
                }
                else
                {
                    trace("FLVPlayer:播放失败!");
                }
            }
            return;
        }// end function

        private function on_close_click(type:MouseEvent) : void
        {
            if (!MainSystem.getInstance().isBusy)
            {
                netStream.pause();
                this.dispatchEvent(new Event(Event.CLOSE));
                MemoryRecovery.getInstance().gcFun(close, MouseEvent.CLICK, on_close_click);
            }
            return;
        }// end function

        private function initNetConnection() : void
        {
            netConnection = new NetConnection();
            netConnection.addEventListener(NetStatusEvent.NET_STATUS, netConnection_NetStatus_handler);
            netConnection.connect(null);
            return;
        }// end function

        private function initBTNClose() : void
        {
            close = new BTNClose();
            close.addEventListener(MouseEvent.CLICK, on_close_click);
            close.y = 10;
            close.x = 780;
            close.buttonMode = true;
            this.addChild(close);
            return;
        }// end function

        private function on_ENTER_FRAME(type:Event) : void
        {
            this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, netStream.bytesLoaded, netStream.bytesTotal));
            if (netStream.bytesLoaded == netStream.bytesTotal)
            {
                this.removeEventListener(Event.ENTER_FRAME, on_ENTER_FRAME);
                this.dispatchEvent(new FLVPlayerEvent(FLVPlayerEvent.COMPLETE));
                if (hasCloseButton && !autoPlay && canSkip == 1)
                {
                    initBTNClose();
                }
            }
            return;
        }// end function

        public function dispose() : void
        {
            MemoryRecovery.getInstance().gcFun(close, MouseEvent.CLICK, on_close_click);
            if (close != null)
            {
                if (close.parent != null)
                {
                    close.parent.removeChild(close);
                }
                close = null;
            }
            MemoryRecovery.getInstance().gcFun(netStream, NetStatusEvent.NET_STATUS, netStream_NetStatusHandler);
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, on_ENTER_FRAME);
            MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, checkIsReady);
            if (netStream != null)
            {
                netStream.pause();
                netStream.close();
            }
            if (netConnection != null)
            {
                netConnection.close();
                MemoryRecovery.getInstance().gcFun(netConnection, NetStatusEvent.NET_STATUS, netConnection_NetStatus_handler);
            }
            this.removeEventListener(Event.ENTER_FRAME, on_ENTER_FRAME);
            video = null;
            netStream = null;
            netConnection = null;
            return;
        }// end function

        private function checkIsReady(type:Event) : void
        {
            if (netStream != null)
            {
                if (netStream.decodedFrames > 0)
                {
                    this.dispatchEvent(new FLVPlayerEvent(FLVPlayerEvent.READY));
                    MemoryRecovery.getInstance().gcFun(this, Event.ENTER_FRAME, checkIsReady);
                }
            }
            return;
        }// end function

        public function resume() : void
        {
            if (netStream != null)
            {
                netStream.resume();
            }
            else
            {
                this.addEventListener(Event.ENTER_FRAME, onEXIT_FRAME);
            }
            return;
        }// end function

    }
}
